package controller;

import com.alibaba.fastjson.JSONObject;
import entity.TeacherTable;
import org.apache.http.entity.ContentType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import service.TeacherTableService;
import utils.MultipartFileDto;
import utils.PageObject;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * (TeacherTable 基础信息功能--老师设置)表控制层
 *
 * @author 张康泽
 * @since 2022-01-17 14:33:21
 */
@RestController
@RequestMapping("/teacherTable")
public class TeacherTableController {
    /**
     * 服务对象
     */
    @Resource
    private TeacherTableService teacherTableService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public TeacherTable selectOne(Integer id) {
        return this.teacherTableService.queryById(id);
    }

    @RequestMapping("/selectallbylimit")
    @CrossOrigin
    @ResponseBody
    public PageObject selectallbylimit(@RequestBody JSONObject pageobj){
        PageObject pageObject = teacherTableService.queryAllByLimit(pageobj);
        return pageObject;
    }
    @RequestMapping("/insertteacher")
    @CrossOrigin
    @ResponseBody
    public JSONObject insertteacher(@RequestBody JSONObject insertobj) throws ParseException {
        JSONObject insert = teacherTableService.insert(insertobj);
        return insert;
    }

    @RequestMapping("/delete")
    @CrossOrigin
    public boolean delete(@RequestBody JSONObject insertobj){
        try {
            return    teacherTableService.deleteById(insertobj.getInteger("pkId"));
        }catch (Exception E){
            return false;
        }


    }


    @ResponseBody
    @CrossOrigin
    @RequestMapping("/importexcel")
    public JSONObject importecxel(@RequestBody JSONObject fileobj) throws Exception {
        System.out.println("========="+fileobj.toString());
        String file = fileobj.getString("file");
        System.out.println(file);
        File excelfile=new File(file);
        FileInputStream fileInputStream=new FileInputStream(file);
        MultipartFile multipartFile=new MultipartFileDto(excelfile.getName(),excelfile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
        JSONObject object = teacherTableService.importtoExcel(multipartFile);
        System.out.println("============="+object.toString());
        return object;
    }
}
